<?php
/**
 * Created by PhpStorm.
 * User: ysk <yanshuaike171211@credithc.com>
 * Date: 2020/5/11
 * Time: 9:14
 */

namespace common\utils;

class Http {
    public static function curlData($curl, $arr, $method = 'POST', $timeout = 60, $is_json = false, $headers = [])
    {
        if (is_string($arr) && !$is_json) {
            $arr1 = json_decode($arr, true);
            if (!empty($arr1)) {
                $arr = $arr1;
            }
        }

        $method = strtoupper($method);
        if(is_array($arr)){
            if ($is_json) {
                $param = json_encode($arr);
            } else {
                $param = !empty($arr) ? http_build_query($arr) : '';//参数
            }
        }else{
            $param = $arr;
        }

        if (!is_array($headers)) {
            $headers = [];
        }

        $conn_timeout = 30; // 连接超时时间

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $curl);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 对认证证书来源的检查
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); // 从证书中检查SSL加密算法是否存在
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $conn_timeout);
        curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
        curl_setopt($ch, CURLOPT_AUTOREFERER, true); // 自动设置Referer
        curl_setopt($ch, CURLOPT_HEADER, false);    //表示bu需要response header
        curl_setopt($ch, CURLOPT_NOBODY, false);   //表示需要response body

        if ($is_json) {
            $headers[] = "Content-Type: application/json; charset=utf-8";
        }

        $req = [];

        if (!empty($headers)) {
            $req['header'] = $headers;
            curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        }

        $url_parts = parse_url($curl);

        switch ($method) {
            case "POST":
                $req['post'] = $arr;
                curl_setopt($ch, CURLOPT_POST, true);//设置POST方式
                curl_setopt($ch, CURLOPT_POSTFIELDS, $param);
                break;
            default:
                if (!empty($arr)) {
                    if (strpos($curl, '?') !== false) {
                        curl_setopt($ch, CURLOPT_URL, $curl . '&' . http_build_query($arr));
                    } else {
                        curl_setopt($ch, CURLOPT_URL, $curl . '?' . http_build_query($arr));
                    }
                }
                if (!empty($url_parts['query'])) {
                    $query_params = [];
                    parse_str($url_parts['query'], $query_params);
                    $req['get'] = array_merge((array)$query_params, (array)$arr);
                } else {
                    $req['get'] = (array)$arr;
                }
                break;        //默认是GET
        }

        $result   = curl_exec($ch);
        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        // 记录错误
        $errno  = curl_errno($ch);
        $errmsg = '';
        if ($errno) {
            $errmsg = curl_error($ch);
        }
        curl_close($ch);
        return $result;
    }
}
